﻿using System;
using System.Linq;
using CoreLibrary;
using CoreLibrary.Paging;
using Entities.DataBase;
using Entities.DataBase.Obj;
using Entities.DataBase.Persistent;
using WebService.Service.Transfer;

namespace WebService.Service {

    public class CostServiceX : AbstractService, ICostServiceX {

        public bool AddCost(string passport, AddCostRequest request) {
            return Run(passport, token => {
                Locate<ICostRepository>().Add(new Cost {
                    Id          = Guid.NewGuid().ToString(),
                    Description = Util.GetString(request.Description),
                    UserId      = token.UserId,
                    Type        = request.Type,
                    Amount      = request.Amount,
                    Status      = request.Status,
                    Currency    = Util.GetString(request.Currency)
                });
                return true;
            });
        }

        public bool UpdateCost(string passport, UpdateCostRequest request) {
            return Run(passport, token => {
                Locate<ICostRepository>().Update(request.Id, new CostUpdator {
                    Amount      = request.Amount,
                    Type        = request.Type,
                    Status      = request.Status,
                    Currency    = Util.GetString(request.Currency),
                    Description = Util.GetString(request.Description)
                });
                return true;
            });
        }

        public bool RemoveCost(string passport, string costId) {
            return Run(passport, token => {
                Locate<ICostRepository>().Remove(costId);
                return true;
            });
        }

        public PagingResult<Cost> ListCosts(string passport, PagingRequest request) {
            return Run(passport, token => {
                var costs = Locate<ICostRepository>().List();
                return new PagingResult<Cost> {
                    Offset = request.Offset,
                    Total  = costs.Count,
                    Data   = costs.Skip(request.Offset)
                                  .Take(request.Limit)
                                  .ToList()
                };
            });
        }
        
    }
}
